﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataContexts.Supplier;

public partial class CarBookingDetails : SearchBookingBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SearchQuery query = new SearchQuery
            {
                CountryId = CountryId,
                RegionId = RegionId,
                LocationId = LocationId,
                PickUpDate = PickUpDate,
                DropDate = DropDate,
                PickUpTime = PickUpTime,
                DropTime = DropTime,
                AdultCapacity = AdultCapacity,
                ChildrenCapacity = ChildrenCapacity,
                LuggageCapacity = LuggageCapacity,
                ACOption = ACOption,
                Transmission = Transmission,
                SupplierVehicleId = SupplierVehicleId,
                SupplierLocationVehicleId = SupplierLocationVehicleId,
                SupplierId = SupplierId
            };

            SearchResults results = query.Execute();
            if (results.Rows.Count == 1)
            {
                ResultRow row = results.Rows[0];

                Location location = supplierContext.Locations.FirstOrDefault(p => p.LocationId == LocationId);

                lblTripDuration.Text = supplierContext.GetTripDays(PickUpDate, DropDate, 0).ToString() + " days";
                imgVehicleImage.ImageUrl = "VehicleImage.ashx?vid=" + row.Vehicle.SupplierVehicleId.ToString();
                lblVehicleCatDesc.Text = row.Vehicle.VehicleCategory + " - " + row.Vehicle.VehicleDescription;
                lblPickUpLocation.Text = location.LocationName;
                lblStartDayDate.Text = PickUpDate.DayOfWeek.ToString() + ", " + PickUpDate.ToString(Constants.FULLDATE_FORMAT) + " " + PickUpTime;
                lblReturnLocation.Text = location.LocationName;
                lblEndDayDate.Text = DropDate.DayOfWeek.ToString() + ", " + DropDate.ToString(Constants.FULLDATE_FORMAT) + " " + DropTime;
                lblAdultCapacity.Text = "x " + (row.Vehicle.AdultCapacity + row.Vehicle.ChildrenCapacity).ToString();
                lblLuggageCapacity.Text = "x " + row.Vehicle.LaggageCapacity.ToString();
                lblNoOfDoors.Text = "x " + row.Vehicle.NoOfDoors.ToString();

                lblAirConditioner.Text = (row.Vehicle.AirCondition.HasValue && row.Vehicle.AirCondition.Value ? "Yes" : "No");
                imgTransimission.ImageUrl = (row.Vehicle.AutoTransmission.HasValue && row.Vehicle.AutoTransmission.Value ? "~/Images/Automatic.jpg" : "~/Images/Manual.jpg");
                lblTransimission.Text = (row.Vehicle.AutoTransmission.HasValue && row.Vehicle.AutoTransmission.Value ? "Automatic" : "Manual");

                trNavigator.Visible = row.Supplier.Navigator.Value;
                trPayOnArrival.Visible = row.Supplier.PayOnArrival.Value;

                if (trPayOnArrival.Visible)
                {
                    chkPayOnArrival.Checked = PayOnArrival;
                }


                chkInsurance.Checked = AvailInsurance;
                chkNavigator.Checked = AvailNavigator;

                trNavOptions.Visible = chkNavigator.Checked;
                if (chkNavigator.Checked)
                {
                    if (AvailNavWithFixPrice)
                        rblNavOptions.SelectedIndex = 1;
                    else
                        rblNavOptions.SelectedIndex = 0;
                }

                decimal bookingFees = row.Total.Value - (row.Price.Value * row.TripDays.Value);
                decimal rentCost = row.Total.Value - bookingFees;
                lblRentalCost.Text = rentCost.ToString(Constants.DECIMAL_FORMAT);
                lblBookingFees.Text = bookingFees.ToString(Constants.DECIMAL_FORMAT);
                lblPayOnArrivalRentCost.Text = lblRentalCost.Text;

                decimal dTotal = row.Total.Value;

                if (trPayOnArrival.Visible && chkPayOnArrival.Checked)
                {
                    dTotal -= rentCost;
                    trPayNowRentCost.Visible = false;
                    trPayOnArrivalHead.Visible = true;
                    trPayOnArrivalRentCost.Visible = true;
                }
                else
                {
                    trPayNowRentCost.Visible = true;
                    trPayOnArrivalHead.Visible = false;
                    trPayOnArrivalRentCost.Visible = false;
                }

                //Pricing
                if (AvailInsurance)
                {
                    SupplierLocationVehicle slv = supplierContext.SupplierLocationVehicles.FirstOrDefault(p => p.SupplierLocationVehicleId == row.SupplierLocationVehicleId);

                    trInsuranceCost.Visible = true;
                    lblInsuranceCost.Text = (slv.InsuranceSellPrice.Value * row.TripDays).ToString();
                    dTotal += (slv.InsuranceSellPrice.Value * row.TripDays.Value);
                }
                else
                {
                    trInsuranceCost.Visible = false;
                }

                if (AvailNavigator && trNavigator.Visible)
                {
                    trNavigatorCost.Visible = true;
                    if (AvailNavWithFixPrice)
                    {
                        lblNavigatorCost.Text = row.Supplier.NavFixRentSellPrice.Value.ToString(Constants.DECIMAL_FORMAT);
                        dTotal += row.Supplier.NavFixRentSellPrice.Value;
                    }
                    else
                    {
                        lblNavigatorCost.Text = (row.Supplier.NavRentSellPrice.Value * row.TripDays).ToString();
                        dTotal += (row.Supplier.NavRentSellPrice.Value * row.TripDays.Value);
                    }
                }
                else
                {
                    trNavigatorCost.Visible = false;
                }

                lblTotalBookingCost.Text = dTotal.ToString(Constants.DECIMAL_FORMAT);
            }
            else
            {
                Response.Redirect("/default.aspx");
            }
        }

    }
    protected void chkInsurance_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("CarBookingDetails.aspx" + Helper.GetRequestUrl("|ains|") + "&ains=" + (chkInsurance.Checked ? "1" : "0"));
    }
    protected void chkNavigator_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("CarBookingDetails.aspx" + Helper.GetRequestUrl("|anv|") + "&anv=" + (chkNavigator.Checked ? "1" : "0"));
    }
    protected void btnBookNow_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("dopayment.aspx" + Helper.GetRequestUrl(""));
    }
    protected void rblNavOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        string anvf = "0";
        if (rblNavOptions.SelectedIndex == 1)
            anvf = "1";

        Response.Redirect("CarBookingDetails.aspx" + Helper.GetRequestUrl("|anvf|") + "&anvf=" + anvf);
    }
    protected void chkPayOnArrival_CheckedChanged(object sender, EventArgs e)
    {
        string poa = "0";
        if (chkPayOnArrival.Checked)
            poa = "1";

        Response.Redirect("CarBookingDetails.aspx" + Helper.GetRequestUrl("|poa|") + "&poa=" + poa);
    }
}
